package org.lys.config;

import org.apache.commons.logging.LogFactory;
import org.lys.model.GlobalResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

@RestControllerAdvice
public class GlobalExceptionHandler {
    private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler(Exception.class)
    public GlobalResult handleException(Exception e, HttpServletRequest request) {
        log.error("请求地址'{}',发生异常", request.getRequestURI(), e);
        return new GlobalResult(500, "系统异常");
    }

    @ExceptionHandler(ArithmeticException.class)
    public GlobalResult handleArithmeticException(HttpServletRequest request, ArithmeticException e) {
        log.error("请求地址'{}',发生异常", request.getRequestURI(), e);
        return new GlobalResult(500, "计算异常");
    }
}
